package com.bobo.web.provider.util;

import com.bobo.web.api.domain.video.VideoTokenDto;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.processing.OperationManager;
import com.qiniu.processing.OperationStatus;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class QiNiuUtils {
    public static final String ACCESS_KEY = "6DCMtcdv46JecTNAe9VET02XdTQCCgytj9T9G8Pb";
    public static final String SECRET_KEY = "R9UoCi8CFW0jObXg_ZxQaiI1F5wVOhIyeODindwi";
    //数据处理队列名称，必须
    public static final String PERSISTENT_PIPELINE = "yangbo-Pipeline";
    @Value("${basketball.qi.niu.buck}")
    public String BASKETBALL_QI_NIU_BUCK;
    @Value("${basketball.qi.niu.domain}")
    public String BASKETBALL_QI_NIU_DOMAIN_OF_BUCKET;
    @Value("${basketball.qi.niu.notify.url}")
    public String BASKETBALL_QI_NIU_NOTIFY_URL;

    /**
     * 获取指定名称的上传凭证
     *
     * @return
     */
    public VideoTokenDto getToken() {
        VideoTokenDto videoTokenDto = new VideoTokenDto();
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String key = String.valueOf(System.currentTimeMillis());
        String upToken = auth.uploadToken(BASKETBALL_QI_NIU_BUCK, key);
        videoTokenDto.setToken(upToken);
        videoTokenDto.setKey(key);
        return videoTokenDto;
    }
    /**
     * 带水印的token
     *
     * @return
     */
    public VideoTokenDto getTokenShuiYin() {
        String fops = "imageView2/0/interlace/1/q/75|watermark/2/text/WUI=/font/6buR5L2T/fontsize/380/fill/IzlDOUNBMw==/dissolve/100/gravity/SouthEast/dx/10/dy/10";
        VideoTokenDto videoTokenDto = new VideoTokenDto();
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String key = String.valueOf(System.currentTimeMillis())+".jpg";
        String pfops = fops +"|saveas/"+ UrlSafeBase64.encodeToString(String.format("%s:%s", BASKETBALL_QI_NIU_BUCK, key+5));
        videoTokenDto.setToken(auth.uploadToken(BASKETBALL_QI_NIU_BUCK, key, 3600,
                new StringMap().putNotEmpty("persistentOps", pfops).putNotEmpty("persistentPipeline", PERSISTENT_PIPELINE), false));
        videoTokenDto.setKey(key);
        return videoTokenDto;
    }

    /**
     * 批量转码
     *
     * @param key
     * @return
     */
    public String transcoding(String key) throws QiniuException {
        //待处理文件名
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        //数据处理指令，支持多个指令
        String shuiYin = "杨波";
        String color = "#9C9CA3";
        String saveMp4Entry = String.format("%s:%s.mp4", BASKETBALL_QI_NIU_BUCK, key);
        String saveJpgEntry = String.format("%s:%s.jpg", BASKETBALL_QI_NIU_BUCK, key);
        String avthumbMp4Fop = String.format("avthumb/mp4/wmText/%s/wmFontColor/%s/wmFontSize/19/wmGravityText/SouthEast|saveas/%s",UrlSafeBase64.encodeToString(shuiYin
        ), UrlSafeBase64.encodeToString(color), UrlSafeBase64.encodeToString(saveMp4Entry));
        String vframeJpgFop = String.format("vframe/jpg/offset/1|saveas/%s", UrlSafeBase64.encodeToString(saveJpgEntry));
        //将多个数据处理指令拼接起来
        String persistentOpfs = StringUtils.join(new String[]{
                avthumbMp4Fop, vframeJpgFop
        }, ";");

        //数据处理队列名称，必须
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //构建持久化数据处理对象
        OperationManager operationManager = new OperationManager(auth, cfg);

//        String persistentId = operationManager.pfop(BASKETBALL_QI_NIU_BUCK, key, persistentOpfs, PERSISTENT_PIPELINE, BASKETBALL_QI_NIU_NOTIFY_URL+"/web/qiniu/storage/persistentNotify",true);
        String persistentId = operationManager.pfop(BASKETBALL_QI_NIU_BUCK, key, persistentOpfs, PERSISTENT_PIPELINE, true);
        //可以根据该 persistentId 查询任务处理进度
        System.out.println(persistentId);

        OperationStatus operationStatus = operationManager.prefop(persistentId);
        //解析 operationStatus 的结果
        return persistentId;
    }


}
